//
//  BaseView.swift
//  FM
//
//  Created by x on 2017/11/8.
//  Copyright © 2017年 HLB. All rights reserved.
//

import UIKit

protocol BaseViewDelegate:NSObjectProtocol {
    
    /// 刷新或则按钮验证码点击事件
    ///
    /// - Parameter tag: <#tag description#>
    func refreshOrVCodeBtuDidClick(tag:NSInteger)
    
    func registerOrConfirmBtuDidClick()
}

class BaseView: UIView {

    weak var delegate:BaseViewDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    /// 初始化
    func setupUI() -> Void {
        
        //1.添加子控件
        addSubview(phoneImage)
        addSubview(phoneTextFiled)
        addSubview(phoneLbale)
        addSubview(registerImage)
        addSubview(GCTextFiled)
        addSubview(GCImage)
        addSubview(refreshGCBtu)
        addSubview(GCLable)
        addSubview(VCImage)
        addSubview(VCTextFiled)
        addSubview(VCLable)
        addSubview(VCBtu)
        addSubview(VCmarkLable)
        addSubview(pwdImage)
        addSubview(pwdTextFiled)
        addSubview(pwdLbale)
        addSubview(confirmBtu)
        
        //2.布局子控件
        
        let dic = ["phoneImage":phoneImage,"phoneTextFiled":phoneTextFiled,"phoneLbale":phoneLbale,
            "registerImage":registerImage,"GCTextFiled":GCTextFiled,"GCImage":GCImage,"refreshGCBtu":refreshGCBtu,"GCLable":GCLable,
                   "VCImage":VCImage,"VCTextFiled":VCTextFiled,"VCLable":VCLable,"VCBtu":VCBtu,"VCmarkLable":VCmarkLable,
                   "pwdImage":pwdImage,"pwdTextFiled":pwdTextFiled,"pwdLbale":pwdLbale,
        "confirmBtu":confirmBtu] as [String:Any]
        
        var cons:[NSLayoutConstraint] = [NSLayoutConstraint]()
        
        //手机号
        cons += NSLayoutConstraint.constraints(withVisualFormat: "H:|-20-[phoneImage(15)]", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: dic)
        cons += NSLayoutConstraint.constraints(withVisualFormat: "V:|-40-[phoneImage(20)]", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: dic)
        
        cons += NSLayoutConstraint.constraints(withVisualFormat: "H:[phoneImage]-10-[phoneTextFiled]-20-|", options: NSLayoutFormatOptions.alignAllCenterY, metrics: nil, views: dic)
        
        cons += NSLayoutConstraint.constraints(withVisualFormat: "H:|-20-[phoneLbale]-20-|", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: dic)
        cons += NSLayoutConstraint.constraints(withVisualFormat: "V:[phoneTextFiled]-20-[phoneLbale(1)]", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: dic)
        
        //图形验证码
        cons += NSLayoutConstraint.constraints(withVisualFormat: "H:|-20-[registerImage(15)]", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: dic)
        cons += NSLayoutConstraint.constraints(withVisualFormat: "V:[phoneLbale]-20-[registerImage(20)]", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: dic)
        
        cons += NSLayoutConstraint.constraints(withVisualFormat: "H:[registerImage]-10-[GCTextFiled]", options: NSLayoutFormatOptions.alignAllCenterY, metrics: nil, views: dic)
        
        cons += NSLayoutConstraint.constraints(withVisualFormat: "H:[GCTextFiled]-5-[GCImage(80)]", options: NSLayoutFormatOptions.alignAllCenterY, metrics: nil, views: dic)
        cons += NSLayoutConstraint.constraints(withVisualFormat: "V:[GCImage(40)]", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: dic)
        
        cons += NSLayoutConstraint.constraints(withVisualFormat: "H:[GCImage]-5-[refreshGCBtu(20)]-30-|", options: NSLayoutFormatOptions.alignAllCenterY, metrics: nil, views: dic)
        cons += NSLayoutConstraint.constraints(withVisualFormat: "V:[refreshGCBtu(20)]", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: dic)
        
        cons += NSLayoutConstraint.constraints(withVisualFormat: "H:|-20-[GCLable]-20-|", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: dic)
        cons += NSLayoutConstraint.constraints(withVisualFormat: "V:[GCTextFiled]-20-[GCLable(1)]", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: dic)
        
        //验证码
        cons += NSLayoutConstraint.constraints(withVisualFormat: "H:|-20-[VCImage(15)]", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: dic)
        cons += NSLayoutConstraint.constraints(withVisualFormat: "V:[GCLable]-20-[VCImage(20)]", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: dic)
        
        cons += NSLayoutConstraint.constraints(withVisualFormat: "H:[VCImage]-10-[VCTextFiled]", options: NSLayoutFormatOptions.alignAllCenterY, metrics: nil, views: dic)
        
        cons += NSLayoutConstraint.constraints(withVisualFormat: "H:[VCTextFiled]-5-[VCLable(1)]", options: NSLayoutFormatOptions.alignAllCenterY, metrics: nil, views: dic)
        cons += NSLayoutConstraint.constraints(withVisualFormat: "V:[VCLable(30)]", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: dic)
        
        cons += NSLayoutConstraint.constraints(withVisualFormat: "H:[VCLable]-5-[VCBtu(100)]-30-|", options: NSLayoutFormatOptions.alignAllTop, metrics: nil, views: dic)
        cons += NSLayoutConstraint.constraints(withVisualFormat: "V:[VCBtu(30)]", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: dic)
        
        cons += NSLayoutConstraint.constraints(withVisualFormat: "H:|-20-[VCmarkLable]-20-|", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: dic)
        cons += NSLayoutConstraint.constraints(withVisualFormat: "V:[VCTextFiled]-20-[VCmarkLable(1)]", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: dic)
        
        //密码
        cons += NSLayoutConstraint.constraints(withVisualFormat: "H:|-20-[pwdImage(15)]", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: dic)
        cons += NSLayoutConstraint.constraints(withVisualFormat: "V:[VCmarkLable]-20-[pwdImage(20)]", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: dic)
        
        cons += NSLayoutConstraint.constraints(withVisualFormat: "H:[pwdImage]-10-[pwdTextFiled]-20-|", options: NSLayoutFormatOptions.alignAllCenterY, metrics: nil, views: dic)
        
        cons += NSLayoutConstraint.constraints(withVisualFormat: "H:|-20-[pwdLbale]-20-|", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: dic)
        cons += NSLayoutConstraint.constraints(withVisualFormat: "V:[pwdTextFiled]-20-[pwdLbale(1)]", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: dic)

        //确认按钮
        cons += NSLayoutConstraint.constraints(withVisualFormat: "H:|-20-[confirmBtu]-20-|", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: dic)
        cons += NSLayoutConstraint.constraints(withVisualFormat: "V:[confirmBtu(40)]", options: NSLayoutFormatOptions.init(rawValue: 0), metrics: nil, views: dic)
        
        
        addConstraints(cons)
        
    }
    
    //MARK: -- 懒加载
    
    private lazy var phoneImage:UIImageView = UIImageView.imageWith(radios: 0, imageName: "register_icon_phone")
    
    //手机号输入框
    fileprivate lazy var phoneTextFiled:UITextField = {
        let textFiled = UITextField()
        textFiled.translatesAutoresizingMaskIntoConstraints = false
        textFiled.placeholder = "请输入手机号"
        textFiled.keyboardType = UIKeyboardType.phonePad
        textFiled.clearButtonMode = UITextFieldViewMode.whileEditing
        
        return textFiled
    }()
    
    private lazy var phoneLbale:UILabel = UILabel.lableWith(backgroundColor: UIColor.RGBColor(r: 204, g: 204, b: 204))
    
    private lazy var registerImage:UIImageView = UIImageView.imageWith(radios: 0, imageName: "register_icon_chongfumim")
    
    //图形验证码输入框
    private lazy var GCTextFiled:UITextField = {
        let textFiled = UITextField()
        textFiled.translatesAutoresizingMaskIntoConstraints = false
        textFiled.placeholder = "请图形验证码"
        textFiled.isSecureTextEntry = true
        textFiled.clearButtonMode = UITextFieldViewMode.whileEditing
        
        return textFiled
    }()
    
    //图形验证码图片
    private lazy var GCImage:UIImageView = UIImageView.imageWith(radios: 0, imageName: "nav_bg")
    
    //刷新图形验证码按钮
    private lazy var refreshGCBtu:UIButton = UIButton.buttonWith(image: "graphics_code", hightImage: "", tag: 1, target: self, action: #selector(refreshVerificationCodeBtuClick(btu:)))
    
    private lazy var GCLable:UILabel = UILabel.lableWith(backgroundColor: UIColor.RGBColor(r: 204, g: 204, b: 204))
    
    private lazy var VCImage:UIImageView = UIImageView.imageWith(radios: 0, imageName: "register_icon_yanzhengma")
    
    //验证码输入框
    fileprivate lazy var VCTextFiled:UITextField = {
        let textFiled = UITextField()
        textFiled.translatesAutoresizingMaskIntoConstraints = false
        textFiled.placeholder = "请输入验证码"
        textFiled.keyboardType = UIKeyboardType.phonePad
        textFiled.clearButtonMode = UITextFieldViewMode.whileEditing
        
        return textFiled
    }()
    
    private lazy var VCLable:UILabel = UILabel.lableWith(backgroundColor: UIColor.RGBColor(r: 204, g: 204, b: 204))
    
    //获取验证码按钮
    fileprivate lazy var VCBtu:UIButton = {
        let btu = UIButton.buttonWith(title: "获取验证码", titleColor: UIColor.white, fontSize: 16, tag: 2, target: self, action: #selector(refreshVerificationCodeBtuClick(btu:)))
        btu.backgroundColor = UIColor.RGBColor(r: 231, g: 103, b: 61)
        
        return btu
    }()
    
    private lazy var VCmarkLable:UILabel = UILabel.lableWith(backgroundColor: UIColor.RGBColor(r: 204, g: 204, b: 204))
    
    private lazy var pwdImage:UIImageView = UIImageView.imageWith(radios: 0, imageName: "register_icon_mima")
    
    fileprivate lazy var pwdTextFiled:UITextField = {
        let textFiled = UITextField()
        textFiled.translatesAutoresizingMaskIntoConstraints = false
        textFiled.placeholder = "请输入6-18位密码"
        textFiled.isSecureTextEntry = true
        textFiled.clearButtonMode = UITextFieldViewMode.whileEditing
        
        return textFiled
    }()
    
    lazy var pwdLbale:UILabel = UILabel.lableWith(backgroundColor: UIColor.RGBColor(r: 204, g: 204, b: 204))

    lazy var confirmBtu:UIButton = {
        let btu = UIButton.buttonWith(title: "确定", titleColor: UIColor.black, fontSize: 16, tag: 3, target: self, action: #selector(loginOrConfirmBtuClick(btu:)))
        btu.backgroundColor = UIColor.RGBColor(r: 204, g: 204, b: 204)
        btu.layer.cornerRadius = 20
        
        return btu
    }()

    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

//MARK: -- 一些按钮点击事件

extension BaseView {

    /// 刷新按钮
    ///
    /// - Parameter btu: <#btu description#>
    func refreshVerificationCodeBtuClick(btu:UIButton) -> Void {
        if btu.tag == 1 {
            
        }else {
            //这里应该发送验证码
            VCBtu.getSMSVerificationCode(time: 60, backgroundColor:UIColor.RGBColor(r: 231, g: 103, b: 61))
            if VCTextFiled.text?.characters.count != 0 {
                SMSSDK.getVerificationCode(by: SMSGetCodeMethod.init(0), phoneNumber: VCTextFiled.text, zone: "86", result: { (error) in
                    if error == nil {
                        print("获取短信验证码成功")
                    }
                })
            }
        }
        delegate?.refreshOrVCodeBtuDidClick(tag: btu.tag)
    }
    
    /// 确认按钮的点击事件
    ///
    /// - Parameter btu: <#btu description#>
    func loginOrConfirmBtuClick(btu:UIButton) -> Void {
        delegate?.registerOrConfirmBtuDidClick()
    }

}






